package com.csw.monkmanager.controller;

import com.csw.monkmanager.annotation.CacheAnnotation;
import com.csw.monkmanager.annotation.DeleteAnnotation;
import com.csw.monkmanager.annotation.LogAnnotation;
import com.csw.monkmanager.dao.ArticleDao;
import com.csw.monkmanager.dao.ArticleRepository;
import com.csw.monkmanager.entity.Article;
import com.csw.monkmanager.service.ArticleService;
import com.csw.monkmanager.util.HttpUtil;
import org.apache.commons.io.FilenameUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("article")
public class ArticleController {
	private static final String DEL = "del";
	@Autowired
	private ArticleService articleService;
	@Autowired
	private ArticleDao articleDao;
	@Autowired
	private ArticleRepository articleRepository;
	@Autowired
	private ElasticsearchRestTemplate elasticsearchRestTemplate;

	/**
	 * @param hotwords
	 * @return
	 */
	@RequestMapping("hotwords")
	public List<Article> hotwords(String hotwords) {
		NativeSearchQuery nativeSearchQuery = new NativeSearchQueryBuilder()
				.withQuery(QueryBuilders.multiMatchQuery(hotwords,"title","content"))
				.withSort(SortBuilders.fieldSort("createDate").order(SortOrder.DESC))
				.withPageable(PageRequest.of(0, 10))
				.withHighlightFields(
						new HighlightBuilder.Field("title").preTags("<span style='color:red'>").postTags("</span>"),
						new HighlightBuilder.Field("content").preTags("<span style='color:red'>").postTags("</span>"))
				.build();
		/* 4.处理查询结果*/
		SearchHits<Article> articles = elasticsearchRestTemplate.search(nativeSearchQuery, Article.class);
		/*得到查询结果返回的内容*/
		List<org.springframework.data.elasticsearch.core.SearchHit<Article>> searchHits = articles.getSearchHits();
		/* 设置一个需要返回的实体类集合*/
		List<Article> discussPosts = new ArrayList<>();
		for (org.springframework.data.elasticsearch.core.SearchHit<Article> searchHit : searchHits) {
			Map<String, List<String>> highLightFields = searchHit.getHighlightFields();
			/*将高亮的内容填充到content中*/
			searchHit.getContent().setTitle(highLightFields.get("title") == null ? searchHit.getContent().getTitle() : highLightFields.get("title").get(0));
			searchHit.getContent().setContent(highLightFields.get("content") == null ? searchHit.getContent().getContent() : highLightFields.get("content").get(0));

			/*放到实体类中*/
			discussPosts.add(searchHit.getContent());

		}

		return discussPosts;
	}

	/**
	 *
	 */
	@RequestMapping("inputclear")
	public void inputclear() {
		articleRepository.deleteAll();
	}

	/**
	 *
	 */
	@RequestMapping("inputreload")
	public void inputreload() {
		List<Article> articles = articleDao.selectAll();
		articleRepository.saveAll(articles);
		// poemlist;;" + articles);
	}

	/**
	 * @param page
	 * @param rows
	 * @return
	 */
	@CacheAnnotation()
	@RequestMapping("showAllArticles")
	public Map showAllArticles(Integer page, Integer rows) {
		Map map = articleService.showAllArticles(page, rows);
		// showAllArticles]" + map);
		return map;
	}

	/**
	 * @param imgFile
	 * @param session
	 * @param request
	 * @return
	 */
	@DeleteAnnotation
	@RequestMapping("uploadImg")
	public Map uploadImg(MultipartFile imgFile, HttpSession session, HttpServletRequest request) {
		HashMap<String, Object> hashMap = new HashMap<>(16);
		String dir = "/upload/articleImg/";
		try {
			String httpUrl = HttpUtil.getHttpUrl(imgFile, request, session, dir);
			hashMap.put("error", 0);
			hashMap.put("url", httpUrl);
		} catch (Exception e) {
			hashMap.put("error", 0);
			hashMap.put("mesage", "上传错误");
			e.printStackTrace();
		}
		return hashMap;
	}

	/**
	 * @param session
	 * @param request
	 * @return
	 */
	@RequestMapping("showAllImgs")
	public Map showAllImgs(HttpSession session, HttpServletRequest request) {
		String realPath = session.getServletContext().getRealPath("/upload/articleImg/");
		HashMap<String, Object> hashMap = new HashMap<>(16);
		ArrayList<Object> arrayList = new ArrayList<>();
		File file = new File(realPath);
		File[] files = file.listFiles();
		for (File file1 : files) {
			HashMap fileMap = new HashMap(8);
			fileMap.put("is_dir", false);
			fileMap.put("has_file", false);
			fileMap.put("filesize", file1.length());
			fileMap.put("is_photo", true);
			String extension = FilenameUtils.getExtension(file1.getName());
			fileMap.put("filetype", extension);
			fileMap.put("filename", file1.getName());
			String s = file1.getName().split("_")[0];
			SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String format = sf.format(new Date(Long.valueOf(s)));
			fileMap.put("datetime", format);
			arrayList.add(fileMap);
		}
		hashMap.put("file_list", arrayList);
		hashMap.put("total_count", arrayList.size());
		hashMap.put("current_url", request.getContextPath() + "/upload/articleImg/");
		return hashMap;
	}

	@RequestMapping("insertArticle")
	@DeleteAnnotation
	public void insertArticle(MultipartFile articleImg, Article article, HttpSession session,
							  HttpServletRequest request) {
		String httpUrl = HttpUtil.getHttpUrl(articleImg, request, session, "/upload/articleImg/");
		String uuid = UUID.randomUUID().toString();
		article.setId(uuid);
		article.setUrl(httpUrl);
		article.setCreateDate(new Date());
		article.setPublishDate(new Date());
		// 状态字段保留
		System.out.println(articleImg);
		System.out.println(article);
		articleService.insertArticle(article);
		// 添加文章的时候同时es里添加
		articleRepository.save(article);
	}

	/**
	 * @param article
	 */
	@RequestMapping("addArticle")
	@DeleteAnnotation
	public void addArticle(Article article) {
		articleService.addArticle(article);
	}

	/**
	 * @param article
	 */
	@RequestMapping("updateArticle")
	public void updateArticle(Article article) {
		// article]" + article);
		articleService.updateArticle(article);
	}

	/**
	 * @param id
	 * @param oper
	 * @param article
	 */
	@RequestMapping("editArticle")
	@DeleteAnnotation()
	public void editArticle(String[] id, String oper, Article article) {
		for (int i = 0; i < id.length; i++) {
			// 数组" + id[i]);
		}
		// 删除的时候es同时删除,先删除
		List<String> strings = Arrays.asList(id);
		for (String ss : strings) {
			// ss]" + ss);
			articleRepository.delete(articleDao.selectByPrimaryKey(new Article().setId(ss)));
		}
		/* 后删除*/
		if (DEL.equals(oper)) {
			articleService.deleteArticle(id);
		}
	}

	/**
	 * @throws Exception
	 */
	@RequestMapping("downloadArticle")
	public void downloadArticle() throws Exception {
		List<Article> articles = articleService.queryAllArticles();
		// 创建 Excel 工作薄对象
		try (HSSFWorkbook workbook = new HSSFWorkbook();) {
			// 创建工作表 参数：表名（相当于Excel的sheet1,sheet2...）
			HSSFSheet sheet = workbook.createSheet("文章信息1");
			// 创建标题行
			HSSFRow row = sheet.createRow(0);
			String[] title = {"ID", "标题", "链接", "内容", "创建时间", "发布时间", "状态", "上师"};
			// 处理单元格对象
			HSSFCell cell = null;
			for (int i = 0; i < title.length; i++) {
				/*单元格下标*/
				cell = row.createCell(i);
				/*单元格内容*/
				cell.setCellValue(title[i]);
			}
			// 处理数据行
			for (int i = 0; i < articles.size(); i++) {
				// 遍历一次创建一行
				HSSFRow row2 = sheet.createRow(i + 1);
				Article ac = articles.get(i);
				// Article article=new
				// Article(ac.getId(),ac.getTitle(),ac.getUrl(),ac.getContent(),ac.getCreate_date(),ac.getPublish_date(),ac.getStatus(),ac.getGuru_id());
				/* 每行对应放的数据*/
				row2.createCell(0).setCellValue(ac.getId());
				row2.createCell(1).setCellValue(ac.getTitle());
				row2.createCell(2).setCellValue(ac.getUrl());
				row2.createCell(3).setCellValue(ac.getContent());
				row2.createCell(4).setCellValue(ac.getCreateDate());
				row2.createCell(5).setCellValue(ac.getPublishDate());
				row2.createCell(6).setCellValue(ac.getStatus());
				row2.createCell(7).setCellValue(ac.getGuruId());
				/**
				 *
				 */
				/* 设置单元格日期格式*/
				Cell cell2 = row2.createCell(4);
				/* 将集合中的数据写入至xls文件中*/
				HSSFDataFormat dataFormat = workbook.createDataFormat();
				short format = dataFormat.getFormat("yyyy-MM-dd");
				HSSFCellStyle cellStyle = workbook.createCellStyle();
				cellStyle.setDataFormat(format);
				/*添加日期样式*/
				cell2.setCellStyle(cellStyle);
				/*添加数据*/
				cell2.setCellValue(ac.getCreateDate());
				/**
				 *
				 */
				/*	 设置单元格日期格式*/
				Cell cell3 = row2.createCell(5);
				cellStyle.setDataFormat(format);
				/* 添加日期样式*/
				cell3.setCellStyle(cellStyle);
				/* 添加数据*/
				cell3.setCellValue(ac.getPublishDate());
			}
			/* 创建输出流 从内存中写入本地磁盘*/
			workbook.write(new FileOutputStream(new File("C:\\Users\\Administrator\\Desktop\\stu.xls")));
			/*关闭资源*/
			workbook.close();
		} finally {

		}

	}

	/**
	 *
	 */
	@LogAnnotation(value = "上传文章信息")
	@RequestMapping("uploadArticle")
	@DeleteAnnotation
	public void uploadArticle() {
		/* articleService.deleteAll();*/
		List<Article> articles = articleDao.selectAll();
		for (int i = 0; i < articles.size(); i++) {
			articleDao.delete(articles.get(i));
		}
		/**
		 *
		 */
		try {
			// 获取要导入的文件
			HSSFWorkbook workbook = new HSSFWorkbook(
					new FileInputStream(new File("C:\\Users\\Administrator\\Desktop\\stu.xls")));
			// 获取工作薄
			HSSFSheet sheet = workbook.getSheet("文章信息1");
			for (int i = 1; i <= sheet.getLastRowNum(); i++) {
				Article article = new Article();

				// 获取行
				HSSFRow row = sheet.getRow(i);

				article.setId(row.getCell(0).getStringCellValue());
				article.setTitle(row.getCell(1).getStringCellValue());
				article.setUrl(row.getCell(2).getStringCellValue());
				article.setContent(row.getCell(3).getStringCellValue());
				article.setCreateDate(row.getCell(4).getDateCellValue());
				article.setPublishDate(row.getCell(5).getDateCellValue());
				article.setStatus(row.getCell(6).getStringCellValue());
				article.setGuruId(row.getCell(7).getStringCellValue());
				// 调用一个插入数据库的方法
				// article]" + article);
				articleService.insertArticle(article);
			}
			// 关闭资源
			workbook.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/** 6. 文章详情接口
	 *
	 * @param article
	 * @return
	 */
	@RequestMapping("article")
	public Map article(Article article) {
		Map map = new HashMap();
		List<Article> articles = articleDao.selectAll();
		map.put("status", 200);
		map.put("article", articles);
		return map;
	}

	/**
	 *
	 */
	@RequestMapping("deleteAllArticle")
	@DeleteAnnotation
	public void deleteAllArticle() {
		articleService.deleteAll();
	}
}
